package vip.guzb.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 任务执行器装配
 *
 * @author 顾志兵
 * @mail ipiger@163.com
 * @since 2024-03-26
 */
@Configuration
public class ExecutorConfiguration {

    // 这里特意取名为 taskExecutor, 这样可以阻止框架对默认TaskExecutor的装配
    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setMaxPoolSize(2);
        // 开启「停机时等待已提交任务的执行完成」特性
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 设置最大等待时长
        taskExecutor.setAwaitTerminationSeconds(120);
        // 初始化任务执行器，此后执行器就进入工作状态了
        taskExecutor.initialize();
        return taskExecutor;
    }

}
